
package com.wwy.monitor.service.impl;

import com.wwy.monitor.common.MessageException;
import com.wwy.monitor.common.PageResult;
import com.wwy.monitor.mapper.tddl.MonitorCaseMapper;
import com.wwy.monitor.mapper.tddl.MonitorTaskMapper;
import com.wwy.monitor.model.enumeration.DeleteEnum;
import com.wwy.monitor.model.tddl.dto.MonitorAppDTO;
import com.wwy.monitor.model.tddl.dto.MonitorCaseDTO;
import com.wwy.monitor.model.tddl.dto.MonitorTaskDTO;
import com.wwy.monitor.model.tddl.query.MonitorCaseQuery;
import com.wwy.monitor.model.tddl.query.MonitorLogQuery;
import com.wwy.monitor.model.tddl.vo.MonitorCaseVO;
import com.wwy.monitor.model.tddl.vo.MonitorConfigDetailVo;
import com.wwy.monitor.model.tddl.vo.MonitorTaskVo;
import com.wwy.monitor.model.vo.MonitorCaseBatchVO;
import com.wwy.monitor.service.*;
import com.wwy.monitor.util.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author long.Wanheng
 * @version V1.00
 * @createDate 2019/11/7
 */
@Service(value = "monitorCaseService")
public class MonitorCaseServiceImpl implements MonitorCaseService {
    @Autowired
    private MonitorCaseMapper monitorCaseMapper;
    @Autowired
    private MonitorTaskMapper monitorTaskMapper;
    @Resource(name = "monitorAppService")
    private MonitorAppService monitorAppService;
    @Resource(name = "monitorConfigDetailService")
    private MonitorConfigDetailService monitorConfigDetailService;
    @Resource
    private MonitorLogService monitorLogService;
    @Resource(name = "caseThreadPool")
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private MonitorTaskService monitorTaskService;

    @Override
    public PageResult queryMonitorCase(MonitorCaseQuery query) {
        if (null != query.getStartTime()) {
            query.setStartDate(new Date(query.getStartTime()));
        }
        if (null != query.getEndTime()) {
            query.setEndDate(new Date(query.getEndTime()));
        }
//å¼å¯åé¡µ
        int pageNum = query.getPageNum();
        int pageSize = query.getPageSize();
        Integer offset = (pageNum - 1) * pageSize;
        query.setPageNum(offset);
        query.setPageSize(pageSize);
        int dataCount = monitorCaseMapper.queryCaseCount(query);
        int pages = (dataCount + pageSize - 1) / pageSize;
        List<MonitorCaseVO> data = monitorCaseMapper.queryCaseList(query);
        PageResult result = new PageResult();
        result.setPageSize(pageSize);
        result.setCurrentPage(pageNum);
        result.setPages(pages);
        result.setTotalCount(dataCount);
        result.setDatas(data);
        return result;
    }

    @Override
    public int addMonitorCase(MonitorCaseBatchVO param) {
//校验
        MonitorAppDTO monitorApp = monitorAppService.selectById(param.getAppId());
        if (monitorApp == null) {
            throw new MessageException("è¯¥åºç¨ä¸å­å¨");
        }
//æ ¹æ®appIdãéç½®è¯¦æidè·åéç½®è¯¦æ
        List<MonitorConfigDetailVo> detailVo = monitorConfigDetailService.selectByAppIdAndIds(param.getAppId(), param.getDetailIdList());
        if (null == detailVo) {
            throw new MessageException("éç½®ä¸å­å¨!");
        }
        List<MonitorTaskDTO> taskList = Lists.newArrayList();
        for (MonitorConfigDetailVo vo : detailVo) {
//å°è£task
            MonitorTaskDTO dto = new MonitorTaskDTO();
            dto.setAppId(param.getAppId());
            dto.setDetailId(vo.getId());
            dto.setTaskName(monitorApp.getAppName() + "_" + vo.getConfigName() + "_" + DateUtil
                    .format(new Date(), DateUtil.DEFAULT_PATTERN));
            dto.setStatus(0);
            dto.setConfigType(vo.getConfigType());
            dto.setCreateUser(param.getCreateUser());
            taskList.add(dto);
        }
//批量添加ä»»å¡
        monitorTaskMapper.addBatchMonitorTask(taskList);
//å°è£caseList
        List<MonitorCaseDTO> caseList = Lists.newArrayList();
        for (MonitorTaskDTO task : taskList) {
            MonitorCaseDTO caseDTO = new MonitorCaseDTO();
            caseDTO.setTaskId(task.getId());
            caseDTO.setDetailId(task.getDetailId());
            caseDTO.setAppId(param.getAppId());
            caseDTO.setIsDelete(DeleteEnum.NOT_DELETE.getValue());
            caseDTO.setCreateUser(param.getCreateUser());
            caseDTO.setUpdateUser(param.getCreateUser());
            caseDTO.setCreateTime(new Date());
            caseDTO.setUpdateTime(new Date());
            caseList.add(caseDTO);
        }
//批量添加case
        int count = monitorCaseMapper.addBatchMonitorCase(caseList);
//        //æ°å¼çº¿ç¨æ§è¡caseéªè¯
//        threadPoolExecutor.submit(() -> {
//            try {
//                monitorTaskService.dealMonitorTaskAndSendMsg(detailVo.getConfigType());
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//        });
        return count;
    }

    @Override
    public PageResult queryCaseTaskLog(Long taskId) {
//monitorTaskMapper.
        MonitorTaskDTO taskVo = monitorTaskMapper.selectById(taskId);
        MonitorLogQuery query = new MonitorLogQuery();
        query.setTaskId(taskVo.getId());
        if (taskVo.getStatus() == 2) {
            query.setStartTime(taskVo.getCreateTime().getTime());
            query.setEndTime(taskVo.getUpdateTime().getTime());
        } else {
            query.setDs(taskVo.getCreateTime());
        }
        return monitorLogService.queryMonitorLog(query);
    }

    @Override
    public int addBatchMonitorCase(List<MonitorCaseDTO> list) {
        return monitorCaseMapper.addBatchMonitorCase(list);
    }

    @Override
    public int deleteById(Long id) {
        int count = 0;
        MonitorCaseDTO dto = monitorCaseMapper.selectById(id);
        if (dto != null) {
            count = monitorCaseMapper.deleteById(id);
            if (count > 0) {
                monitorTaskMapper.deleteById(dto.getTaskId());
            }
        }
        return count;
    }

    @Override
    public int deleteByIds(List<Integer> ids) {
        return monitorCaseMapper.deleteByIds(ids);
    }
}